package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.support.v4.widget.ImageViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.TalentPeopleBean;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.TalentListActivity;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/6/14.
 */

public class TalentPersonsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;

    private int FOOT_VIEW = -1;
    private int NARMAL_VIEW = 0;
    private long cycleItemId;
    private int allCount;

    private List<TalentPeopleBean> data = new ArrayList<>();
    public TalentPersonsAdapter (Context context, long cycleItemId){
        this.context = context;
        this.cycleItemId = cycleItemId;
    }

    public List<TalentPeopleBean> getData() {
        return data;
    }

    public void setData(List<TalentPeopleBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == (allCount -1)){//是最后的view
            return FOOT_VIEW;
        }
        return NARMAL_VIEW;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == FOOT_VIEW){
            view = LayoutInflater.from(context).inflate(R.layout.layout_cycle_talent_more, parent, false);
            return new TalentPersonsFootViewVH(view);
        }else {
            view = LayoutInflater.from(context).inflate(R.layout.layout_talent_persons, parent, false);
            return new TalentPersonsVH(view);
        }


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        if (position <data.size()){

            TalentPersonsVH holder = (TalentPersonsVH) h;
            final TalentPeopleBean bean = data.get(position);

            ImageManager.getInstance().loadImage(context, holder.head, bean.getUserAvatar(), ImageManager.ImageType.CIRCLE);
            holder.name.setText(bean.getNickName());
            holder.school.setText(bean.getUniName());

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                    intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/"+bean.getUid());
                    context.startActivity(intent);
                }
            });

            holder.talent_num.setText((position+1) +"");
            switch (position){
                case 0:
                    ImageViewCompat.setImageTintList(holder.talent_image, ColorStateList.valueOf(context.getResources().getColor(R.color.talent_1)));
                    break;
                case 1:
                    ImageViewCompat.setImageTintList(holder.talent_image, ColorStateList.valueOf(context.getResources().getColor(R.color.uni_title_bar)));
                    break;
                case 2:
                    ImageViewCompat.setImageTintList(holder.talent_image, ColorStateList.valueOf(context.getResources().getColor(R.color.talent_2)));
                    break;
                default:
                    ImageViewCompat.setImageTintList(holder.talent_image, ColorStateList.valueOf(context.getResources().getColor(R.color.white)));
                    break;
            }


        }else {
            TalentPersonsFootViewVH holder = (TalentPersonsFootViewVH) h;
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TalentListActivity.class);
                    intent.putExtra(BaseConfig.CYCLE_ITEM_ID, cycleItemId);
                    context.startActivity(intent);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        allCount = data.size() + 1;
        return allCount ;
    }

    class TalentPersonsFootViewVH extends RecyclerView.ViewHolder {
        private View itemView;

        public TalentPersonsFootViewVH(View view) {
            super(view);
            itemView = view;
            AutoUtils.auto(view);
        }
    }

    class TalentPersonsVH extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView head, talent_image;
        private TextView name, school, talent_num;

        public TalentPersonsVH(View view) {
            super(view);
            itemView = view;
            AutoUtils.auto(view);

            talent_image = view.findViewById(R.id.talent_image);
            head = view.findViewById(R.id.head);
            name = view.findViewById(R.id.name);
            school = view.findViewById(R.id.school);
            talent_num = view.findViewById(R.id.talent_num);
        }
    }
}
